package Trip;

import com.alibaba.fastjson.JSONObject;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class MapTest extends Mapper<LongWritable, Text, Map_Trip, NullWritable> {
    Map_Trip k = new Map_Trip();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        JSONObject jsonObject = JSONObject.parseObject(value.toString());
        String[] datas = new String[13];
        datas[0] = jsonObject.getString("start_loc");
        datas[1] = jsonObject.getString("end_loc");
        datas[2] = jsonObject.getString("price");
        datas[3] = jsonObject.getString("save");
        datas[4] = jsonObject.getString("line");
        datas[5] = jsonObject.getString("hotel");
        datas[6] = jsonObject.getString("room");
        datas[7] = jsonObject.getString("start_company");
        datas[8] = jsonObject.getString("start_type");
        datas[9] = jsonObject.getString("start_time");
        datas[10] = jsonObject.getString("end_comany");
        datas[11] = jsonObject.getString("end_type");
        datas[12] = jsonObject.getString("end_time");
        //去空
        for (String data : datas) {
            if (data.equals("") || data == null) {
                return;
            }
        }
        //去除价格异常值
        if (Integer.parseInt(datas[2]) < 1000 || Integer.parseInt(datas[2]) > 3500) {
            return;
        }
        //去除节省异常值
        if (Integer.parseInt(datas[2]) <= Integer.parseInt(datas[3])) {
            return;
        }
        //截取酒店名称
        datas[5] = datas[5].split(" ")[0];
        //输出
        k.set(datas[0], datas[1], datas[2], datas[3], datas[4], datas[5], datas[6], datas[7], datas[8], datas[9], datas[10], datas[11], datas[12]);
        context.write(k, NullWritable.get());
    }
}
